﻿namespace game.display.hud
{

    public class HUDTimer : lumo.IUpdatable, lumo.display.object2D.IObject2D, lumo.display.object2D.IPositionable2D
    {

        protected Microsoft.Xna.Framework.Graphics.SpriteFont _SpriteFont;
        protected lumo.Counter _Counter;
        protected lumo.display.object2D.Label _Label;
        protected lumo.display.animation2D.ParallelAnimation2D _Animation;

        public bool Finished { get { return _Counter.Finished; } }

        public HUDTimer(Microsoft.Xna.Framework.Graphics.SpriteFont spriteFont, float milliseconds)
        {
            _SpriteFont = spriteFont;
            if (_SpriteFont == null)
                throw new System.Exception("You can't set this sprite font to null");
            _Counter = new lumo.Counter(0, milliseconds / 1000, milliseconds);
            _Label = new lumo.display.object2D.Label(((int)(_Counter.Value / 60)).ToString("00") + ":" + ((int)(_Counter.Value % 60)).ToString("00"), _SpriteFont);
            _Label.Origin = _Label.Size / 2;
            _Animation = new lumo.display.animation2D.ParallelAnimation2D(_Label);
            _Animation.Add(new lumo.display.animation2D.ColorAnimation2D(Microsoft.Xna.Framework.Color.Red, milliseconds, lumo.Smooth.Start));
            _Animation.Add(new lumo.display.animation2D.ScaleAnimation2D(new Microsoft.Xna.Framework.Vector2(2, 2), milliseconds));
        }

        #region IUpdateable

        public void Update(lumo.LumoComponent component)
        {
            _Counter.Update(component);
            _Label.Text = ((int)(_Counter.Value / 60)).ToString("00") + ":" + ((int)(_Counter.Value % 60)).ToString("00");
            _Animation.Update(component);
        }

        #endregion

        #region IObject2D

        public void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch) { _Label.Draw(spriteBatch); }

        protected float _DrawOrder = 0f;

        public float DrawOrder
        {
            get { return _DrawOrder; }
            set { _DrawOrder = value; }
        }

        public bool Visible
        {
            get { return _Label.Visible; }
            set { _Label.Visible = value; }
        }

        #endregion

        #region IPositionable2D

        public Microsoft.Xna.Framework.Vector2 Position
        {
            get { return _Label.Position; }
            set { _Label.Position = value + _Label.Origin; }
        }

        public float X
        {
            get { return Position.X; }
            set { Position = new Microsoft.Xna.Framework.Vector2(value, Y); }
        }

        public float Y
        {
            get { return Position.Y; }
            set { Position = new Microsoft.Xna.Framework.Vector2(X, value); }
        }

        #endregion

    }

}